package apps.softtech.Utility;

import java.util.ArrayList;
import org.json.JSONArray;
import android.content.Context;

public class HttpQueue {
	
	Context  mContext;
	ArrayList<QueueItem> listQueueItems;
	
	public HttpQueue(Context context){
		listQueueItems = new ArrayList<QueueItem>();
		mContext = context;
	}
	
	public void pushToQueue(QueueItem item){
		
	}
	
	public QueueItem popFromQueue(){
		QueueItem item = null;
		return item;
	}
	
	public void saveToDatabaseQueue(){
		try{
			JSONArray arrData = new JSONArray();
			
			for(int i = 0 ; i < listQueueItems.size() ; i++ ){
				arrData.put(listQueueItems.get(i).toJSONObject());
			}
			FileUtility fie = new FileUtility();
			fie.SaveContentFile(mContext, ApplicationConstants.QUEUE_FILE, arrData.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public ArrayList<QueueItem> getQueueFromOldData(){
		listQueueItems = new ArrayList<QueueItem>();
		
		try{
			FileUtility fie = new FileUtility();
			String old_data = fie.LoadContentFile(mContext , ApplicationConstants.QUEUE_FILE);
			JSONArray arrQueue = new JSONArray(old_data);
			for(int i = 0 ; i < arrQueue.length() ; i++ ){
				try{
					listQueueItems.add(new QueueItem(arrQueue.getJSONObject(i)));
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return listQueueItems;
	}
	
	public void excuteOneQueueItem(QueueItem item ){
		if(item== null || item.equals(null)){
			return;
		}
		
		
		
		item.submitToServer(mContext);
		listQueueItems.remove(item);
	}
	
	public void excuteQueue(){
		
	}
	
	
	
}
